#!/bin/bash
#
#       Author  : anthony
#       Date    : 2023/06/27
#    Description: redis keys batch delete
#
#    1.desc. redis cluster will delete multiple keys , must login each node ,
#   and the key can not on this node, this delete will hard.
#    2. check redis cluster master nodes
#    3. edit redis_del.sh script
#    4. use redis_del.sh script
#
# redis-cli command variables
REDIS_CLI=/usr/local/redis/bin/redis-cli

# check redis master nodes info
# ./redis-cli -c -h ip1 -p port -a "password" cluster nodes | grep master

# redis cluster master nodes variables（3master,3slave）
REDIS_SERVER1=192.168.3.2
REDIS_SERVER2=192.168.3.3
REDIS_SERVER3=192.168.3.4

# redis cluster password
REDIS_PWD="password"

$REDIS_CLI -c -h $REDIS_SERVER1 -a "$REDIS_PWD" keys $1 | xargs -i $REDIS_CLI -c -h $REDIS_SERVER1 -a "$REDIS_PWD" del {}
$REDIS_CLI -c -h $REDIS_SERVER2 -a "$REDIS_PWD" keys $1 | xargs -i $REDIS_CLI -c -h $REDIS_SERVER2 -a "$REDIS_PWD" del {}
$REDIS_CLI -c -h $REDIS_SERVER3 -a "$REDIS_PWD" keys $1 | xargs -i $REDIS_CLI -c -h $REDIS_SERVER3 -a "$REDIS_PWD" del {}

# arguments comment:
#   -c : start redis cluster mode to login redis cluster
#   -h : redis host address
#   xargs -i : -i options tell xargs use {} to pass arguments

# Demo
# sh redis_del.sh a_*
#  a_* : as will delete a_ prefix key or *ab suffix key




